import {observable, action} from "mobx";
import {ListPage} from "weaver-mobile-page";
import {Bubble, Tools} from "weaver-mobile";
import {Base64} from "js-base64";
import ReplyEditorStore from "../reply/replyEditorStore";
import * as API from "../../api"

const getLabel = Tools.getLabel;

export default class HomeStore {
    @observable isLoading = true;
    @observable visualHeight;
    @observable spaceH;
    @observable pageIndex = 1;
    @observable pageCount = 2;
    @observable scrollHeight = 0;
    @observable refreshing = false;
    @observable pageGetting = false;
    @observable countInfo = {};
    @observable ApprovalTaskList = new ListPage.Store();
    @observable ExchangeList = new ListPage.Store();
    @observable currentPage = 1;
    @observable comment = new ReplyEditorStore();
    @action
    init = () => {
        this.pageIndex = 1;
        this.pageCount = 2;
        this.pageGetting = false;
        this.getPortalCount();
        this.getApprovalTaskList();
        this.currentPage = 1;
        this.comment = new ReplyEditorStore({
            getComment: this.getReplyDatas,
        });
        this.comment.commentStore.getDatas();
    };

    @action
    getPortalCount = () => {
        API.getPortalCount().then(res => {
            this.countInfo = res;
        })
    };
    @action
    getApprovalTaskList = () => {
        API.getApprovalTaskList().then(action(res => {
            if (res.datas) {
                this.ApprovalTaskList.setState({data: res.datas});
            }
        }))
    };

    getReplyDatas = () => {
        this.isLoading = false;
        const params = {
            pageindex: this.currentPage++
        }
        return API.getExchangeList(params).then(action(res => {
            const result = res;
            result.replyList.forEach(v => {
                let imgArr = [];
                let content = Base64.decode(v.content);                                 //解码
                let src = content.match(/\bsrc\b\s*=\s*[\'\"]?([^\'\"\s]*)[\'\"]?/ig);  //查找img
                content = content.replace(/<img\b.*?(?:\>|\/>)/gi, "");                 //去除remark 中上传的img（带有fileid字段）
                const aTags = content.match(/<a\b[^>]+\bhref="([^"]*)"[^>]*>([\s\S]*?)<\/a>/gi) //搜索所有a标签
                if (aTags) {
                    aTags.forEach(v => {
                        const href = v.match(/<a\b[^>]+\bhref="([^"]*)"[^>]*>([\s\S]*?)<\/a>/)    //查找href和value，并替换href
                        if (href) {
                            content = content.replace(href[1], "javascript:void(0)");
                        }
                    })
                }
                v.content = Base64.encode(content)                                      //编码
                if (src !== null) {
                    src.forEach(v => {
                        const fileid = v.match(/fileid=([a-zA-Z0-9]*)/i)                      //获取fileid
                        if (fileid) {
                            const temp = Array.from(fileid);
                            imgArr.push(temp[1]);                                       //拼接成数组
                        }
                    })
                }
                v.imgArr = imgArr;
            })
            return result
        }))
    }

    @action
    setVisualHeight = (h) => {
        this.visualHeight = h;
    }
    @action
    onPageGetting = (bool) => {
        this.pageGetting = bool;
    }
    @action
    pageIndexChange = (number) => {
        this.pageIndex = number;
    }
    @action
    getScrollHeight = (h) => {
        this.scrollHeight = h;
    }

    @action
    approve = (id) => {
        const params = {
            method: "approve",
            taskids: id
        }
        API.doTaskApproval(params).then(action(res => {
            if (res.success) {
                Bubble.success(getLabel("1009", "审批通过"), 1)
                this.init()
            } else {
                Bubble.error(res.msg, 1);
            }
        }))
    }
    @action
    refuse = (id) => {
        const params = {
            method: "refuse",
            taskids: id
        }
        API.doTaskApproval(params).then(action(res => {
            if (res.success) {
                Bubble.success(getLabel("500830", "任务已退回"), 1)
                this.init()
            } else {
                Bubble.error(res.msg, 1);
            }
        }))
    }
}
